package ypmits.components.util {
	import ypmits.font.Arial;

	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.GridFitType;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * @author Dave Lenz
	 */
	public class TextFieldUtility extends Sprite {

		public var textfield : TextField;
		private var _textformat : TextFormat;
		private var _text : String;
		private var _html : Boolean = false;

		
		
		public function TextFieldUtility( text : String, fontname : String = null, size : Number = 12, color : uint = 0, bold : Boolean = false, align : String = "left", clickable : Boolean = false, extraclickspace : Number = 0 )
		{
			this._text = text;
			
			textfield = new TextField( );
			textfield.autoSize = align;
			textfield.embedFonts = true;
			textfield.antiAliasType = AntiAliasType.ADVANCED;
			textfield.gridFitType = GridFitType.PIXEL;
			textfield.textColor = color;
			textfield.selectable = false;
			textfield.multiline = true;
			textfield.wordWrap = false;
			_html ? textfield.htmlText = text : textfield.text = text;
			
			_textformat = new TextFormat( !fontname ? new Arial( ).fontName : fontname );
			_textformat.size = size;
			_textformat.color = color;
			_textformat.bold = bold;
			_textformat.align = align;
			
			textfield.setTextFormat( _textformat );
			textfield.defaultTextFormat = _textformat;
			addChild( textfield );
			
			if( clickable ) {
				var r1 : Rectangle = textfield.getCharBoundaries( 0 );
				var r2 : Rectangle = textfield.getCharBoundaries( textfield.length - 1 );
				var r : Rectangle;
				if( r1 ) {
					r = r1.union( r2 );
				} else {
					r = new Rectangle( 0, 0, 10, 10 );
				}
				
				if( extraclickspace ) {
					r.x -= extraclickspace;
					r.y -= extraclickspace;
					r.width += (extraclickspace * 2);
					r.height += (extraclickspace * 2);
				}
				
				var s : Sprite = new Sprite( );
				s.graphics.clear( );
				s.graphics.beginFill( 0xFF0000, 0 );
				s.graphics.drawRect( r.x, r.y, r.width, r.height );
				addChildAt( s, 0 );

				mouseChildren = false;
				buttonMode = true;
			}
		}

		
		
		override public function set width(value : Number) : void {
			textfield.wordWrap = true;
			textfield.width = value;
		}

		
		
		override public function set height(value : Number) : void {
			textfield.wordWrap = true;
			textfield.height = value;
		}

		
		
		public function set debug( value : Boolean ) : void {
			textfield.border = value;
			textfield.borderColor = 0xFF9900;
		}

		
		
		public function get html() : Boolean 
		{
			return _html;
		}

		
		
		public function set html( value : Boolean ) : void {
			_html = value;
			if( value ) {
				if( _text ) textfield.htmlText = _text;
			} else {
				if( _text ) textfield.text = _text;
			}
		}

		
		
		public function get text() : String
		{
			return _text;
		}

		
		
		public function set text( value : String ) : void {
			_text = value;
			_html ? textfield.htmlText = _text : textfield.text = _text;
		}

		
		
		public function get type() : String
		{
			return textfield.type;
		}

		
		
		public function set type( value : String ) : void {
			textfield.type = value;
		}

		
		
		public function set displayAsPassword( value : Boolean ) : void {
			textfield.displayAsPassword = value;
		}

		
		
		public function get leading() : Object
		{
			return _textformat.leading;
		}

		
		
		public function set leading( value : Object ) : void {
			_textformat.leading = value;
			textfield.setTextFormat( _textformat );
		}

		
		
		public function get sharpness() : Number
		{
			return textfield.sharpness;
		}

		
		
		public function set sharpness( value : Number ) : void {
			textfield.sharpness = value;
		}

		
		
		public function get kerning() : Object
		{
			return _textformat.kerning;
		}

		
		
		public function set kerning( value : Object ) : void {
			_textformat.kerning = value;
		}

		
		
		public function get multiline() : Boolean 
		{
			return textfield.multiline;
		}

		
		
		public function set multiline(value : Boolean) : void {
			textfield.multiline = value;
			textfield.wordWrap = value ? false : true;
		}

		
		
		public function get thickness() : Number
		{
			return textfield.thickness;
		}

		
		
		public function set thickness(thickness : Number) : void {
			textfield.thickness = thickness;
		}

		
		
		public function get gridFitType() : String 
		{
			return textfield.gridFitType;
		}

		
		
		public function set gridFitType( gridFitType : String ) : void {
			textfield.gridFitType = gridFitType;
		}


		
		public function get antiAliasType() : String 
		{
			return textfield.antiAliasType;
		}

		
		
		public function set antiAliasType( antiAliasType : String ) : void {
			textfield.antiAliasType = antiAliasType;
		}
	}
}
